package com.fenghongzhang.module_main.glide.glide
import com.bumptech.glide.annotation.GlideExtension
import com.bumptech.glide.annotation.GlideOption
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.BaseRequestOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.fenghongzhang.lib_net.R
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


/**
 *@author Wcj
 *@description
 *@date 2022/6/20 21:08
 */
@GlideExtension
object GlideExtension {

    //加载头像
    @JvmStatic
    @GlideOption
    fun applyAvatar(options: BaseRequestOptions<*>): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .error(R.mipmap.icon_default_avatar)
            .placeholder(R.mipmap.icon_default_avatar)
            .dontAnimate()
            .apply(RequestOptions().transform(CenterCrop(), CircleCrop()))
    }

    //加载圆角图片
    @JvmStatic
    @GlideOption
    fun applyRoundPic(
        options: BaseRequestOptions<*>,
        radius: Int,
        cornerType: RoundedCornersTransformation.CornerType,
        size: Int
    ): BaseRequestOptions<*> {

        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .format(DecodeFormat.PREFER_RGB_565)
            .override(size)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .transform(CenterCrop(), RoundedCornersTransformation(radius, 0, cornerType))
            .format(DecodeFormat.PREFER_RGB_565)
            .dontAnimate()
    }

    //加载圆角图片
    @JvmStatic
    @GlideOption
    fun applyRoundPic(
        options: BaseRequestOptions<*>,
        radius: Int,
        cornerType: RoundedCornersTransformation.CornerType,
        width: Int,
        height: Int
    ): BaseRequestOptions<*> {

        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .format(DecodeFormat.PREFER_RGB_565)
            .override(width, height)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .transform(CenterCrop(), RoundedCornersTransformation(radius, 0, cornerType))
            .dontAnimate()
    }

    @JvmStatic
    @GlideOption
    fun applyRoundPic(
        options: BaseRequestOptions<*>,
        radius: Int,
        cornerType: RoundedCornersTransformation.CornerType,
    ): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .format(DecodeFormat.PREFER_RGB_565)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .transform(CenterCrop(), RoundedCornersTransformation(radius, 0, cornerType))
            .dontAnimate()
    }

    //加载gif
    @JvmStatic
    @GlideOption
    fun applyGif(options: BaseRequestOptions<*>): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.DATA)
            .error(R.drawable.shape_bg_mask_round_4dp)


    }

    //加载PhotoView
    @JvmStatic
    @GlideOption
    fun applyPhotoView(options: BaseRequestOptions<*>): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .format(DecodeFormat.PREFER_RGB_565)
            .dontAnimate()

    }

    @JvmStatic
    @GlideOption
    fun applyPortraitVideoFirstFrame(options: BaseRequestOptions<*>): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .format(DecodeFormat.PREFER_RGB_565)
            .override(101, 180)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .frame(1_000_000)
            .transform(RoundedCornersTransformation(4, 0, RoundedCornersTransformation.CornerType.ALL))


    }

    @JvmStatic
    @GlideOption
    fun applyLandscapeVideoFirstFrame(options: BaseRequestOptions<*>): BaseRequestOptions<*> {
        return options
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .format(DecodeFormat.PREFER_RGB_565)
            .error(R.drawable.shape_bg_mask_round_4dp)
            .override(340, 180)
            .frame(1_000_000)
            .transform(CenterCrop(), RoundedCornersTransformation(4, 0, RoundedCornersTransformation.CornerType.ALL))


    }

}